﻿using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace HardwareWebApi.Controllers
{
    public class OuterImgMiddleware
    {
        public static string RootPath { get; set; } // 配置文件读取绝对位置
        private readonly RequestDelegate _next;

        public OuterImgMiddleware(RequestDelegate next, IHostingEnvironment env)
        {
            _next = next;
        }

        public async Task Invoke(HttpContext context)
        {
            var path = context.Request.Path.ToString();
            var headersDictionary = context.Request.Headers;

            if (context.Request.Method == "GET")
            {
                if (!string.IsNullOrEmpty(path) && path.Contains("/upload/logo"))
                {
                    // var unauthorizedImagePath = Path.Combine(RootPath, path);
                    var unauthorizedImagePath = RootPath + path;
                    await context.Response.SendFileAsync(unauthorizedImagePath);
                    return;
                }

                await _next(context);
            }
        }
    }
}
